<template>
    <div>
        <h3>注册</h3>
        <el-row>
            <el-col :span="8"></el-col>
            <el-col :span="8">
                <el-form :model="this.form" label-width="120px">
                    <el-form-item label="手机号">
                        <el-input v-model="this.form.telphone"></el-input>
                    </el-form-item>
                    <el-form-item label="验证码">
                        <el-input v-model="this.form.otpCode"></el-input>
                    </el-form-item>
                    <el-form-item label="姓名">
                        <el-input v-model="this.form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="性别">
                        <el-radio-group v-model="this.form.gender">
                            <el-radio label="1">男</el-radio>
                            <el-radio label="0">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="年龄">
                        <el-input v-model="this.form.age"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="this.form.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="register()">注册</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
export default {
    data() {
        return {
            form: {
                telphone: '',
                otpCode: '',
                name: '',
                gender: '1',
                age: '',
                password: '',
            }
        }
    },
    methods: {
        register() {
            if (this.form.telphone == "" || this.form.telphone == null) {
                alert("手机号不能为空");
                return false;
            };
            if (this.form.otpCode == "" || this.form.otpCode == null) {
                alert("验证码不能为空");
                return false;
            };
            if (this.form.name == "" || this.form.name == null) {
                alert("姓名不能为空");
                return false;
            };
            if (this.form.gender == null) {
                alert("性别不能为空");
                return false;
            };
            if (this.form.age == "" || this.form.age == null) {
                alert("年龄不能为空");
                return false;
            };
            if (this.form.password == "" || this.form.password == null) {
                alert("密码不能为空");
                return false;
            };
            this.$http({
                method: 'POST',
                url: this.$backend + "/user/register",  
                data: this.form,
                withCredentials: true,
            })
            .then((result) => {
                if (result.data.status == "success") {
                    alert("注册成功");
                    this.$router.push('/')
                } else {
                    console.log(result);
                    alert("注册失败,"+ result.data.data.errMsg);
                }
            }).catch((err) => {
                console.log(err)
                alert("发送信息超时, 请稍后再试");
                return false;
            });
        }
    },
}

</script>
